package com.example.finance_manager.pages.manage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import com.example.finance_manager.R;
import com.example.finance_manager.dao.SalaryDao;
import com.example.finance_manager.models.Salary;

public class IncomeManageActivity extends AppCompatActivity {

    private EditText etMoney;
    private EditText etOther;
    private EditText etPay;
    private EditText etTime;
    private EditText etType;
    private Button btnDelete;
    private Button btnCancel;
    private Button btnUpdate;
    private Salary salary;
    private SalaryDao salaryDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_income);

        salaryDao = new SalaryDao(this);

        initView();
        initData();
        initListener();
    }

    private void initView() {
        etMoney = (EditText) findViewById(R.id.et_manage_income_money);
        etOther = (EditText) findViewById(R.id.et_manage_income_other);
        etPay = (EditText) findViewById(R.id.et_manage_income_pay);
        etTime = (EditText) findViewById(R.id.et_manage_income_time);
        etType = (EditText) findViewById(R.id.et_manage_income_type);

        btnDelete = (Button) findViewById(R.id.btn_manage_income_delete);
        btnCancel = (Button) findViewById(R.id.btn_manage_income_cancel);
        btnUpdate = (Button) findViewById(R.id.btn_manage_income_update);
    }

    private void initData() {
        Intent intent = this.getIntent();
        salary = (Salary) intent.getSerializableExtra("salary");
        etMoney.setText(salary.getMoney().toString());
        etOther.setText(salary.getOther());
        etPay.setText(salary.getPay());
        etTime.setText(salary.getTime());
        etType.setText(salary.getType());
    }

    private void initListener() {
        btnDelete.setOnClickListener(view -> {
            int res = salaryDao.deleteById(salary.getId());
            System.out.println(res);
            finish();
        });
        btnUpdate.setOnClickListener(view -> {
            String money, other, pay, time, type;
            money = etMoney.getText().toString();
            other = etOther.getText().toString();
            pay = etPay.getText().toString();
            time = etTime.getText().toString();
            type = etType.getText().toString();
            Salary _salary = new Salary(time, pay, type, other, Double.parseDouble(money));
            _salary.setId(salary.getId());
            System.out.println(_salary);
            salaryDao.update(_salary);
            finish();
        });
        btnCancel.setOnClickListener(view -> {
            finish();
        });
    }
}